package com.qzsoft.zhjd.materiel.modules.materielsupplier.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.qzsoft.jeemis.common.utils.Result;
import com.qzsoft.jeemis.common.validator.ValidatorUtils;
import com.qzsoft.jeemis.common.validator.group.AddGroup;
import com.qzsoft.jeemis.common.validator.group.UpdateGroup;
import com.qzsoft.zhjd.common.enums.BizCode;
import com.qzsoft.zhjd.common.enums.YesOrNo;
import com.qzsoft.zhjd.common.utils.RenExceptionUtils;
import com.qzsoft.zhjd.common.utils.ResultUtils;
import com.qzsoft.zhjd.materiel.modules.materielapi.aop.MaterielApiUpdate;
import com.qzsoft.zhjd.materiel.modules.materielsupplier.dto.MaterielSupplierImportDto;
import com.qzsoft.zhjd.materiel.modules.materielsupplier.dto.MaterielSupplierListDto;
import com.qzsoft.zhjd.materiel.modules.materielsupplier.entity.MaterielSupplierEntity;
import com.qzsoft.zhjd.materiel.modules.materielsupplier.service.MaterielSupplierService;

/**
 * @ClassName MaterielSupplierController
 * @Description 供应商控制器
 * @author linhuawei
 * @since 2022-03-30
 */
@RestController
@RequestMapping("/api/materielSupplier")
public class MaterielSupplierController {

    @Autowired
    private MaterielSupplierService materielSupplierService;

    /**
     * @Title page
     * @Description 获取供应商分页列表
     * @param params 查询条件
     * @return 供应商分页列表
     */
    @RequiresPermissions("materiel:supplier:page")
    @GetMapping("/page")
    public Result<IPage<MaterielSupplierListDto>> page(@RequestParam Map<String, Object> params) {
        IPage<MaterielSupplierListDto> page = materielSupplierService.page(params);
        return ResultUtils.ok(page);
    }

//    /**
//     * 访问url获取、保存过磅类型数据
//     * @Author wang_hyu
//     * @date :2022年1月18日
//     */
//    @GetMapping("/urlSaveInfo")
//    @RequiresPermissions("materiel:supplier:page")
//    public Result<String> getUrlInfo() throws IOException {
//        materielSupplierService.getUrlInfo();
//        Result<String> results = ResultUtils.ok();
//        return results;
//    }

    /**
     * @Title list
     * @Description 获取供应商列表
     * @return 供应商列表
     */
    @GetMapping("/list")
    public Result<List<MaterielSupplierEntity>> list(@RequestParam Map<String, Object> params) {
        List<MaterielSupplierEntity> list = materielSupplierService.list(params);
        return ResultUtils.ok(list);
    }

    /**
     * @Title info
     * @Description 获取数据对象
     * @param id 数据主键
     * @return 数据对象
     */
    @RequiresPermissions("materiel:supplier:update")
    @GetMapping("/info/{id}")
    public Result<MaterielSupplierEntity> info(@PathVariable("id") String id) {
        MaterielSupplierEntity entity = materielSupplierService.getById(id);
        if (null == entity) {
            RenExceptionUtils.throwEx(BizCode.DATA_NOT_FOUND, "数据不存在");
        }
        return ResultUtils.ok(entity);
    }

    /**
     * @Title save
     * @Description 添加数据
     * @param entity 需要添加的数据
     * @return 添加结果
     */
    @MaterielApiUpdate("getCooperates")
    @RequiresPermissions("materiel:supplier:save")
    @PostMapping("/save")
    public Result<String> save(@RequestBody MaterielSupplierEntity entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        materielSupplierService.save(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title update
     * @Description 修改数据
     * @param entity 需要修改的数据
     * @return 修改结果
     */
    @MaterielApiUpdate("getCooperates")
    @RequiresPermissions("materiel:supplier:update")
    @PostMapping("/update")
    public Result<String> update(@RequestBody MaterielSupplierEntity entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        materielSupplierService.updateById(entity);
        return ResultUtils.ok();
    }

    /**
     * @Title disable
     * @Description 启用供应商
     * @param id 供应商主键
     * @return 操作结果
     */
    @MaterielApiUpdate("getCooperates")
    @RequiresPermissions("materiel:supplier:update")
    @PostMapping("/enable/{id}")
    public Result<String> enable(@PathVariable("id") String id) {
        materielSupplierService.updateEnable(id, YesOrNo.yes);
        return ResultUtils.ok();
    }

    /**
     * @Title disable
     * @Description 停用供应商
     * @param id 供应商主键
     * @return 操作结果
     */
    @MaterielApiUpdate("getCooperates")
    @RequiresPermissions("materiel:supplier:update")
    @PostMapping("/disable/{id}")
    public Result<String> disable(@PathVariable("id") String id) {
        materielSupplierService.updateEnable(id, YesOrNo.no);
        return ResultUtils.ok();
    }

    /**
     * @Title delete
     * @Description 删除数据
     * @param id 数据主键
     * @return 删除结果
     */
    @MaterielApiUpdate("getCooperates")
    @RequiresPermissions("materiel:supplier:delete")
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable("id") String id) {
        materielSupplierService.removeById(id);
        return ResultUtils.ok();
    }

    /**
     * @Title export
     * @Description 供应商导出
     * @param params 查询条件
     */
    @RequiresPermissions("materiel:supplier:export")
    @GetMapping("/export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        materielSupplierService.export(params, response);
    }

    /**
     * @Title template
     * @Description 下载模板
     */
    @RequiresPermissions("materiel:supplier:template")
    @GetMapping("/template")
    public void template(HttpServletResponse response) {
        materielSupplierService.template(response);
    }

    /**
     * @Title template
     * @Description 下载模板
     */
    @MaterielApiUpdate("getCooperates")
    @RequiresPermissions("materiel:supplier:import")
    @PostMapping("/import")
    public Result<List<MaterielSupplierImportDto>> importExcel(String baseCode, MultipartFile file) {
        if (ObjectUtils.isEmpty(baseCode)) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "基地名称不可为空");
        }
        if (null == file || 0 == file.getSize()) {
            RenExceptionUtils.throwEx(BizCode.PARAMS_ERROR, "上传文件不可为空");
        }
        List<MaterielSupplierImportDto> result = materielSupplierService.importExcel(baseCode, file);
        return ResultUtils.ok(result);
    }
}
